import random


class Address:
    def __init__(self, country, province, street, house_number):
        self.country = country
        self.province = province
        self.street = street
        self.house_number = house_number


class User:
    def __init__(self, account, name, password, grade, address, balance, bank):
        self.account = account
        self.name = name
        self.password = password
        self.grade = grade
        self.address = address
        self.balance = balance
        self.bank = bank



class Bank:
    def __init__(self, bank_name):
        self.bank_name = bank_name
        self.users = {}
        self.max_users = 100

    def add_user(self, user):
        if user.account in self.users:
            return 2
        if len(self.users) >= self.max_users:
            return 3
        self.users[user.account] = user
        return 1

    def deposit(self, account, amount):
        if account not in self.users:
            return False
        self.users[account].balance += amount
        return True

    def withdraw(self, account, password, amount):
        if account not in self.users:
            return 1
        if self.users[account].password != password:
            return 2
        if self.users[account].balance < amount:
            return 3
        self.users[account].balance -= amount
        return 0

    def transfer(self, from_account, to_account, password, amount):
        if from_account not in self.users or to_account not in self.users:
            return 1
        if self.users[from_account].password != password:
            return 2
        if self.users[from_account].balance < amount:
            return 3
        else:
            self.users[from_account].balance -= amount
            self.users[to_account].balance += amount
            return 0


    def query_account(self, account, password):
        if account not in self.users:
            print("该用户不存在。")
            return
        if self.users[account].password != password:
            print("密码错误。")
            return
        user = self.users[account]
        print(f"当前账号：{user.account},密码:{user.password},余额：{user.balance}元，用户居住地址：{user.address.country}{user.address.province}{user.address.street}{user.address.house_number}，当前账户的开户行：{user.bank}")


def generate_account():
    account = ""
    for i in range(8):
        account += str(random.randint(0, 9))
    return account


def main():
    bank = Bank("中国农业银行的昌平沙河支行")
    while True:
        print("************************************")
        print("*         中国工商银行               *")
        print("*         账号管理系统               *")
        print("*            V1.0                 *")
        print("************************************")
        print("1.开户 2.存钱 3.取钱 4.转账 5.查询 6.Bye!")
        choice = input("请选择业务(1 - 6): ")
        if choice == '1':
            name = input("请输入姓名: ")
            password = input("请输入6位密码: ")
            country = input("请输入国家: ")
            province = input("请输入省份: ")
            street = input("请输入街道: ")
            house_number = input("请输入门牌号: ")
            grade = input("输入账户类型")
            address = Address(country, province, street, house_number)
            account = generate_account()
            user = User(account, name, password,grade, address, 0, bank.bank_name)
            result = bank.add_user(user)
            if result == 1:
                print("开户成功，您的账号是:", account)
            elif result == 2:
                print("用户已存在。")
            else:
                print("用户库已满。")
        elif choice == '2':
            account = input("请输入账号: ")
            amount = int(input("请输入存钱金额: "))
            if bank.deposit(account, amount):
                print("存钱成功。")
            else:
                print("账号不存在，存钱失败。")
        elif choice == '3':
            account = input("请输入账号: ")
            password = input("请输入密码: ")
            amount = int(input("请输入取钱金额: "))
            result = bank.withdraw(account, password, amount)
            if result == 0:
                print("取钱成功。")
            elif result == 1:
                print("账号不存在。")
            elif result == 2:
                print("密码错误。")
            else:
                print("钱不够。")
        elif choice == '4':
            from_account = input("请输入转出账号: ")
            to_account = input("请输入转入账号: ")
            password = input("请输入转出账号密码: ")
            amount = int(input("请输入转出金额: "))

            flag2 = True
            while flag2:
                if bank.users[from_account].grade == '金卡':
                    if amount > 50000:
                        amount = int(input("金卡账户最大转出50000，请重新输入转账金额"))
                    else:
                        flag2 = False
                if bank.users[from_account].grade == '普通卡':
                    if amount > 20000:
                        amount = int(input("普通卡账户最大转出20000，请重新输入转账金额"))
                    else:
                        flag2 = False
            result = bank.transfer(from_account, to_account, password, amount)
            if result == 0:
                print("转账成功。")
            elif result == 1:
                print("账号不对。")
            elif result == 2:
                print("密码不对。")
            else:
                print("钱不够。")
        elif choice == '5':
            account = input("请输入账号: ")
            password = input("请输入密码: ")
            bank.query_account(account, password)
        elif choice == '6':
            print("感谢使用，再见！")
            break
        else:
            print("无效选择，请重新输入。")


main()


